package com.gdut.huayan.controller;

import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.po.Conversion;
import com.gdut.huayan.entity.po.ConversionActivity;
import com.gdut.huayan.service.ConversionActivityService;
import com.gdut.huayan.service.ConversionService;
import com.gdut.huayan.service.FileService;
import com.gdut.huayan.validation.group.GroupAdd;
import com.gdut.huayan.validation.group.GroupUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/conversion")
public class ConversionController {

    @Autowired
    private ConversionService conversionService;

    @Autowired
    private ConversionActivityService conversionActivityService;

    @Autowired
    private FileService fileService;

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/activity/add")
    public Result addActivity(@RequestBody @Validated({Default.class, GroupAdd.class}) ConversionActivity conversionActivity,
                              String requestId) {
        conversionActivity.setImageFileId(fileService.getFileStatus(requestId, true));
        conversionActivityService.save(conversionActivity);
        return Result.success();
    }

    /**
     * @return 返回历史记录
     */
    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/activity/getAll")
    public Result getPageActivity(@RequestBody PageQuery pageQuery, Integer status, Integer type) {
        return Result.success(conversionActivityService.getAll(pageQuery, status, type));
    }

    /**
     * @return 返回最新的三皈和五戒
     */
    @PreAuthorize("permitAll()")
    @GetMapping("/activity/get")
    public Result getActivity() throws UnsupportedEncodingException {
        return Result.success(conversionService.get());
    }


    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @DeleteMapping("/activity/delete")
    public Result deleteActivity(Long conversionActivityId) {
        return Result.success(conversionActivityService.removeById(conversionActivityId));
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/activity/update")
    public Result updateActivity(@RequestBody @Validated({Default.class, GroupUpdate.class}) ConversionActivity conversionActivity) {
        conversionActivityService.updateById(conversionActivity);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/add")
    public Result addConversion(@RequestBody @Validated({Default.class, GroupAdd.class}) Conversion conversion, String requestId) {
        conversion.setImageFileId(fileService.getFileStatus(requestId, true));
        conversionService.save(conversion);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/get")
    public Result getAllConversion(@RequestBody PageQuery pageQuery, Integer status) {
        //用户获取自己的converison
        return Result.success(conversionService.getUserConversion(pageQuery, status));
    }

    /* @GetMapping("/get")
     public Result getConversion(Long conversionId){
         return Result.success(conversionService.getById(conversionId));
     }
     */
    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/getByActivityId")
    public Result getConversionByActivityId(Long activityId, Integer status, @RequestBody PageQuery pageQuery) {
        return Result.success(conversionService.getByActivityId(activityId, status, pageQuery));
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/update")
    public Result updateConversionById(@RequestBody @Validated({Default.class, GroupUpdate.class}) Conversion conversion) {
        conversionService.updateById(conversion);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/scan/participate")
    public Result participateConversion(String number) {
        //扫码参加皈依接口
        conversionService.updateById(conversionService.selectByNumber(number));
        return Result.success();
    }

}
